using ConfigWebApi.Common;
using Microsoft.EntityFrameworkCore;
using ConfigWebApi.Repo;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// 支持配置文件热加载
builder.Services.AddSingleton(new ConfigurationBuilder().AddJsonFile("appsettings.json",false,true).Build());
builder.Services.AddDbContext<DemoContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("Production")));

var cfg = builder.Configuration;

// 读取配置表
Console.WriteLine($"【Table】     {cfg["AppName"]}");
Console.WriteLine($"【Table】     {cfg["Logging:LogLevel:Default"]}");

// 读取段落
Console.WriteLine($"【Section】   {cfg.GetSection("JWT")["Issuer"]}");
Console.WriteLine($"【Section】   {cfg.GetSection("ConnectionStrings")["Default"]}");

// 读取段落值
Console.WriteLine($"【Value】     {cfg.GetValue<int>("JWT:Expire")}");
Console.WriteLine($"【Value】     {cfg.GetValue<string>("Logging:LogLevel:Default")}");

// 读取对象
Console.WriteLine($"【Object.1】  {cfg.GetSection("JWT").Get<Jwt>()}");
Console.WriteLine($"【Object.1】  {cfg.GetSection("JWT").Get<Jwt>().Audience}");

// 或绑定对象
var jwt = new Jwt();
cfg.GetSection("JWT").Bind(jwt);
Console.WriteLine($"【Object.2】  {jwt}");

// 内置方法(读取连接字符串)
Console.WriteLine($"【Fast】      {cfg.GetConnectionString("Default")}");
Console.WriteLine();



var app = builder.Build();
app.MapControllers();
app.Run();